import java.util.Arrays;

public class StationRecorder {
    public int getFrameNum = 0;
    //队列长度分布\等待时间分布
    public int queueNum = 0;
    public int sendAttemptNum = 0;
    public int sendSuccessfullyNum = 0;
    public int frameNum = DistributeGenerator.MaxFramePerStation;
    public int[] frameWaitNum = new int[frameNum * 2];
    public long[] frameWaitTime = new long[frameNum * 2];

    public void getFrame() {
        queueNum++;
    }

    public void sendFrameSuccessfully() {
        queueNum--;
        sendSuccessfullyNum++;
        sendAttemptNum = 0;
    }

    public void attemptToSend() {
        sendAttemptNum++;
    }

    public void getOneFrame() {
        getFrameNum++;
    }

    //接收到一个新的帧时设置
    public void setFrameWaitNum(int queueNum) {
        frameWaitNum[getFrameNum] = queueNum;
    }

    //新的一个帧开始发送时设置
    public void setFrameWaitTime(long waitTime) {
        frameWaitTime[sendSuccessfullyNum] = waitTime;
    }

    int cntOfNum = 1;
    int cntOfTime = 1;

    //TODO:由于数组的项偏大,所以会有初始化出来的0没被赋值过
    public void getDistribution() {
        int[] getFrameWaitNum = new int[getFrameNum];
        long[] getFrameWaitTime = new long[sendSuccessfullyNum];
        System.arraycopy(frameWaitNum, 0, getFrameWaitNum, 0, getFrameNum);
        System.arraycopy(frameWaitTime, 0, getFrameWaitTime, 0, sendSuccessfullyNum);
        Arrays.sort(getFrameWaitNum);
        Arrays.sort(frameWaitTime);
        for (int i = 1; i < getFrameNum; i++) {
            if (getFrameWaitNum[i] != getFrameWaitNum[i - 1]) {
                cntOfNum++;
            }
        }
        for (int i = 1; i < sendSuccessfullyNum; i++) {
            if (getFrameWaitTime[i] != getFrameWaitTime[i - 1]) {
                cntOfTime++;
            }
        }
        int[] element1 = new int[cntOfNum];
        int[] num1 = new int[cntOfNum];
        long[] element2 = new long[cntOfTime];
        int[] num2 = new int[cntOfTime];
        for (int i = 0; i < cntOfNum; i++) {
            num1[i] = 1;
        }
        int j1 = 0;
        element1[0] = getFrameWaitNum[0];
        //队列长度
        for (int i = 1; i < getFrameNum; i++) {
            if (getFrameWaitNum[i] == getFrameWaitNum[i - 1]) {
                element1[j1] = getFrameWaitNum[i];
                num1[j1]++;
            } else {
                j1++;
                element1[j1] = getFrameWaitNum[i];
            }
        }

        //等待时间
        for (int i = 0; i < cntOfTime; i++) {
            num2[i] = 1;
        }
        int j2 = 0;
        element2[0] = getFrameWaitTime[0];
        for (int i = 1; i < sendSuccessfullyNum; i++) {
            if (getFrameWaitTime[i] == getFrameWaitTime[i - 1]) {
                element2[j2] = getFrameWaitTime[i];
                num2[j2]++;
            } else {
                j2++;
                element2[j2] = getFrameWaitTime[i];
            }
        }

        System.out.println("总共出现的队列长度:" + cntOfNum);
        for (int i = 0; i < cntOfNum; i++) {
            System.out.print(element1[i] + ":" + num1[i] + ";\t");
        }
        System.out.println();
        double pQueue0 = (double) num1[0] / getFrameNum;
        System.out.println("p(0):" + pQueue0);

        System.out.println("总共出现的等待时间:" + cntOfTime);
        for (int i = 0; i < cntOfTime; i++) {
            System.out.print(element2[i] + ":" + num2[i] + ";\t");
        }
        System.out.println();

    }
}
